package com.njxzc.demo.service.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njxzc.demo.common.MyUtil;
import com.njxzc.demo.common.http.ResponseResult;
import com.njxzc.demo.entity.CartEntity;
import com.njxzc.demo.entity.GoodsEntity;
import com.njxzc.demo.mapper.admin.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, GoodsEntity> implements GoodsService{
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public ResponseResult<Map<String, Object>> getGoods(GoodsEntity goodsEntity) {
        if(goodsEntity.getCurrentPage() == null || goodsEntity.getCurrentPage() < 1)
            goodsEntity.setCurrentPage(1);
        IPage<GoodsEntity> iPage = new Page<>(goodsEntity.getCurrentPage(), 5);

        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(goodsEntity.getGname() != null){
            lambdaQueryWrapper.like(GoodsEntity::getGname, goodsEntity.getGname());
        }
        if(goodsEntity.getGoodstypeId() != null){
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodsEntity.getGoodstypeId());
        }

        //分页查询
        IPage<GoodsEntity> page = goodsMapper.iPageGoods(iPage, lambdaQueryWrapper);
        Map<String, Object> myres = new HashMap<>();
        myres.put("allGoods", page.getRecords());
        myres.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<Map<String, Object>> add(GoodsEntity goodsEntity) {
        byte[] myfile = goodsEntity.getLogoFile();

        if(myfile != null && myfile.length > 0){
            String path = "C:\\Users\\86199\\training\\ebusinessDemo\\src\\assets";
            //获得上传文件原名
            String fileName = goodsEntity.getFileName();
            //对文件重命名
            String fileNewName = MyUtil.getNewFileName(fileName);
            File filePath = new File(path + File.separator + fileNewName);
            //如果文件目录不存在，创建目录
            if(!filePath.getParentFile().exists()) {
                filePath.getParentFile().mkdirs();
            }
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(filePath);
                out.write(myfile);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            if("add".equals(goodsEntity.getAct())) {
                boolean result = save(goodsEntity);
                if(result)//成功
                    return ResponseResult.getMessageResult(null, "A001");
                else
                    return ResponseResult.getMessageResult(null, "A002");
            }else {//修改
                boolean result = updateById(goodsEntity);
                if(result)
                    return ResponseResult.getMessageResult(null, "A001");
                else
                    return ResponseResult.getMessageResult(null, "A002");
            }
        }else{
            return ResponseResult.getErrorResult("A002");
        }
    }

    @Override
    public ResponseResult<Map<String, String>> delete(GoodsEntity goodsEntity) {
        if(goodsEntity.getId() != null){
            boolean resFlag = removeById(goodsEntity.getId());
            if(resFlag){ // 成功
                return ResponseResult.getMessageResult(null, "A001");
            }
            return ResponseResult.getMessageResult(null, "A002");
        }else{
            return ResponseResult.getMessageResult(null, "A002");
        }
    }

    @Override
    public ResponseResult<Map<String, Object>> iPageMyFocusGoods(GoodsEntity goodsEntity) {
        if(goodsEntity.getCurrentPage() == null || goodsEntity.getCurrentPage() < 1)
            goodsEntity.setCurrentPage(1);
        IPage<GoodsEntity> iPage = new Page<>(goodsEntity.getCurrentPage(), 2);

        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if(goodsEntity.getId() != null){
            lambdaQueryWrapper.eq(GoodsEntity::getId, goodsEntity.getId());
        }

        //分页查询
        IPage<GoodsEntity> page = goodsMapper.iPageMyFocusGoods(iPage, lambdaQueryWrapper);
        Map<String, Object> myres = new HashMap<>();
        myres.put("allFocusGoods", page.getRecords());
        myres.put("totalPage", page.getPages());
        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<List<Map<String, Object>>> myCartGoods(CartEntity cartEntity) {
        List<Map<String, Object>> res = goodsMapper.myCartGoods(cartEntity.getBusertableId());
        return ResponseResult.getSuccessResult(res);
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getAdvGoods() {
        List<GoodsEntity> myres = lambdaQuery().eq(GoodsEntity::getIsAdvertisement, 1)
                .orderByDesc(GoodsEntity::getId)
                .last("limit 5")
                .list();
        return ResponseResult.getSuccessResult(myres);
    }

    @Override
    public ResponseResult<List<GoodsEntity>> getGoodsIndex(GoodsEntity goodsEntity) {
        LambdaQueryWrapper<GoodsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //条件构造
        if(goodsEntity.getId() != null && goodsEntity.getGoodstypeId() != null){
            lambdaQueryWrapper.eq(GoodsEntity::getGoodstypeId, goodsEntity.getGoodstypeId());
        }
        if(goodsEntity.getGname() != null && goodsEntity.getGname().trim().isEmpty()){
            lambdaQueryWrapper.like(GoodsEntity::getGname, goodsEntity.getGname());
        }
        lambdaQueryWrapper.orderByDesc(GoodsEntity::getId).last("limit 10");

        return ResponseResult.getSuccessResult(list(lambdaQueryWrapper));
    }

    @Override
    public ResponseResult<GoodsEntity> getGoodsById(GoodsEntity goodsEntity) {
        return ResponseResult.getSuccessResult(getById(goodsEntity.getId()));
    }
}
